﻿using System.Collections.Generic;
using System.Linq;
using Sumen.Business.Data;
using Sumen.Library.Enum;
using Sumen.Library.Interface;
using Sumen.Model.Model;

namespace Sumen.Business.Permission
{
    public class Permission : IPermission
    {
        private string _FormCode;
        private Dao dao;

        private string Users { get; set; }
        private sys_object SysObject { get; set; }

        private sys_user_permission sysUserPermission;
        private List<sys_role_permission> listSysRolePermission;


        public Permission(string pFormCode, string pUser)
        {
            _FormCode = pFormCode;
            Users = pUser;

            dao = new Dao();
            SysObject = dao.GetById<sys_object>(pFormCode);
            sysUserPermission = dao.GetById<sys_user_permission>(Users, _FormCode);

            listSysRolePermission = new List<sys_role_permission>();

            user users = dao.GetById<user>(Users);
            if (users != null)
            {
                dao.CollectionObject(users, "userroles");
                ICollection<userrole> listUserroles = users.userroles;
                if (listUserroles != null)
                {
                    foreach (var listUserrole in listUserroles)
                    {
                        dao.ReferenceObject(listUserrole, "sys_role");
                        dao.CollectionObject(listUserrole.sys_role, "sys_role_permission");
                        listSysRolePermission.AddRange(dao.FindByMultiColumnAnd<sys_role_permission>(new[] { "RP_Object_ID" }, _FormCode));
                        //listSysRolePermission.AddRange(listUserrole.sys_role.sys_role_permission.Where(p => p.RP_Object_ID == _FormCode));
                    }
                }
            }
        }




        protected bool getPermission(EnumFormStatus pEnumFormStatus)
        {
            if (sysUserPermission != null)
            {
                if (sysUserPermission.UP_Permission == null)
                {
                    return false;
                }

                var abc = sysUserPermission.UP_Permission & (int)pEnumFormStatus;
                if ((sysUserPermission.UP_Permission & (int)pEnumFormStatus) != 0)
                    return true;
                else
                    return false;
            }
            else
            {
                if (listSysRolePermission.Count > 0)
                {
                    List<sys_role_permission> obj =
                        listSysRolePermission.Where(p => (p.RP_Permission & (int)pEnumFormStatus) == 0).ToList();
                    if (obj.Count > 0)
                        return false;
                    else
                        return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public bool PermissionView()
        {
            return getPermission(EnumFormStatus.View);
        }

        public bool PermissionAdd()
        {
            return getPermission(EnumFormStatus.Add);
        }

        public bool PermissionUpdate()
        {
            return getPermission(EnumFormStatus.Modify);
        }

        public bool PermissionDelete()
        {
            return getPermission(EnumFormStatus.Delete);
        }

        public bool PermissionPrint()
        {
            return getPermission(EnumFormStatus.Print);
        }

        public bool PermissionWord()
        {
            return getPermission(EnumFormStatus.Word);
        }

        public bool PermissionExcel()
        {
            return getPermission(EnumFormStatus.Excel);
        }

        public bool PermissionApproved()
        {
            return getPermission(EnumFormStatus.Approved);
        }

        public bool PermissionReport()
        {
            return getPermission(EnumFormStatus.Report);
        }


        public int PermissionAll()
        {
            int uP_Permission = 0;
            uP_Permission += PermissionView() ? (int)EnumFormStatus.View : 0;
            uP_Permission += PermissionAdd() ? (int)EnumFormStatus.Add : 0;
            uP_Permission += PermissionUpdate() ? (int)EnumFormStatus.Modify : 0;
            uP_Permission += PermissionDelete() ? (int)EnumFormStatus.Delete : 0;
            uP_Permission += PermissionPrint() ? (int)EnumFormStatus.Print : 0;
            uP_Permission += PermissionWord() ? (int)EnumFormStatus.Word : 0;
            uP_Permission += PermissionExcel() ? (int)EnumFormStatus.Excel : 0;
            uP_Permission += PermissionReport() ? (int)EnumFormStatus.Report : 0;
            uP_Permission += PermissionApproved() ? (int)EnumFormStatus.Approved : 0;
            return uP_Permission;
        }









    }
}
